<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Invitation Class
 *
 * @category  Home
 */
class InvitationAction extends HomeAction
{

    protected $Invitation;
    

	function _initialize()
	{
		
		parent::_initialize();
		$this->Invitation = D('Invitation');
		
		if (empty($this->_user['user_id']) || !$this->_userGroup['allow_invite'])
		{
			$this->error(L('no_perms_action'));
		}
	}


    //邀请码主页
    public function index()
    {
    	
        import('ORG.Util.Page');
    	
        $parameter = '';
        $invite = $map = $order = array();
		$types = array('valid', 'expired', 'used');
		$type = in_array($_GET['type'], $types) ? $_GET['type'] : 'valid'; 
    	
		$parameter .= '&type='. $type;
    	$map['user_id'] = $this->_user['user_id'];
    	
    	//本周可用邀请码
		$invite['credits'] = $this->Invitation->getCredits($this->_user['user_id']);
    	$invite['next_credit'] = $invite['credits'] > 0 ? time() : get_week_day_time(1, 1);

    	switch ($type)
    	{
    		case 'valid':
    			$map['status'] = 1;
    			break;
    		case 'expired':
    			$map['expire_time'] = array(array('gt', 0), array('lt', time()));
    			break;
    		case 'used':
    			$map['status'] = 0;
    			$map['register_user_id'] = array('neq', 0);
    			break;
    	}
    	
    	$order['create_time'] = 'desc';

    	$total = $this->Invitation->where($map)->count();
    	$Page  = new Page($total, $pageSize);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
    	$invitationKeys = $this->Invitation->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

    	$seo = array(
    		'title' => L('invitation'),
    		'keywords' => '',	
    		'description' => '',
    	);
    	
    	$this->assign('seo', $seo);
    	$this->assign('type', $type);
    	$this->assign('invite', $invite);
    	$this->assign('invitationKeys', $invitationKeys);
    	$this->assign('pagination', $pagination);
    	$this->display();
    }
    
    
    //生成邀请码
    public function makeKey()
    {
    	$credits = $this->Invitation->getCredits($this->_user['user_id']);
    	if (0 == $credits)
    	{
    		$this->error(L('no_perms_action'));
    	}
    	
    	if ($this->isPost())
    	{
			$this->Invitation->makeKey($this->_user['user_id']);
			$this->success(L('invite_key_made'));    		
    	}
    	$this->redirect('Invitation/index');
    }
    
    
    //邀请用户
	public function inviteFriend()
	{
		
	    $user = $this->_user;

	    $captcha = require_captcha_when('email', $this->_user['user_group']);

		//邮件发送限制检查
        if (exceed_activity_limit($this->_user['user_id'], $this->_user['user_group'], 'email'))
        {
            $this->error(L('exceed_email_limit'));
        }

		if ($this->isPost())
		{
		    $emails = explode(',', $_POST['recipient']);
			foreach ($emails as $email)
			{
				if(!validate_email(trim($email)))
				{
					$this->error(L('email_invalid'));
				}
			}

			//验证码检查
			if ($captcha && md5($_POST['verify']) != $_SESSION['verify'])
			{
				$this->error(L('captcha_incorrect'));
			}

			$recipient = implode(',', $emails);

			$Mailer = A('Mailer');
			$result = $Mailer->friendInvitation($this->_user['user_id'], $recipient, $_POST['message']);
			if ($result)
			{
				$this->success(L('send_email_ok'));
			}
			else
			{
			 	$this->error(L('send_email_failed'));
			}
		}
		else
		{
		    $hash = kg_auth_code($user['user_id'], 'encode');
		    $inviteLink = get_site_domain() . U('Account/login', array('inviter' => $hash));
		    $user['invite_link'] = $inviteLink;
		    
		    $invitePoints = get_activity_points('invite');
		    L('invite_friend_tips', sprintf(L('invite_friend_tips'), $invitePoints));

		    $this->assign('captcha', $captcha);
			$this->assign('user', $user);
			$this->display();
		}
	}
	
}

/* End of file InvitationAction.class.php */
/* Location: ./App/Lib/Action/Home/InvitationAction.class.php */